package com.siao.example.libaray.mvc.view
{
	import com.siao.component.ComboBox;
	import com.siao.data.ArrayList;
	import com.siao.example.common.ui.CommonButton;
	import com.siao.example.common.ui.Layer;
	import com.siao.example.libaray.mvc.method.MVCMethod;
	import com.siao.example.libaray.mvc.ui.BlockComboItem;
	import com.siao.mvc.View;
	import com.siao.utils.string.trim;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	/**
	 * MVC视图
	 * @author SiaoLeon
	 */
	public class MVCView extends View
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var label_Title:TextField;
		
		private var label_Queue:TextField;
		
		private var label_Block:TextField;
		
		private var label_params:TextField;
		
		private var txtQueue:TextField;
		
		private var txtParams:TextField;
		
		private var txtCombo:TextField;
		
		private var block:ComboBox;
		
		private var btnSend:CommonButton;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function MVCView()
		{
			super();
			
			this.label_Title = new TextField();
			this.label_Title.autoSize = TextFieldAutoSize.LEFT;
			this.label_Title.text = "发送一个MVC消息，（如果设定队列，则将循环发送三次）";
			this.label_Title.x = 10;
			this.label_Title.y = 80;
			
			this.label_Queue = new TextField();
			this.label_Queue.autoSize = TextFieldAutoSize.LEFT;
			this.label_Queue.text = "队列：";
			this.label_Queue.x = 10;
			this.label_Queue.y = 110;
			
			this.label_Block = new TextField();
			this.label_Block.autoSize = TextFieldAutoSize.LEFT;
			this.label_Block.text = "阻塞：";
			this.label_Block.x = 150;
			this.label_Block.y = 110;
			
			this.label_params = new TextField();
			this.label_params.autoSize = TextFieldAutoSize.LEFT;
			this.label_params.text = "参数：";
			this.label_params.x = 250;
			this.label_params.y = 110;
			
			this.txtQueue = new TextField();
			this.txtQueue.type = TextFieldType.INPUT;
			this.txtQueue.border = true;
			this.txtQueue.x = 50;
			this.txtQueue.y = 110;
			this.txtQueue.width = 90;
			this.txtQueue.height = 18;
			
			this.txtParams = new TextField();
			this.txtParams.type = TextFieldType.INPUT;
			this.txtParams.border = true;
			this.txtParams.x = 290;
			this.txtParams.y = 110;
			this.txtParams.width = 200;
			this.txtParams.height = 18;
			
			this.txtCombo = new TextField();
			this.txtCombo.defaultTextFormat = new TextFormat("simsun", 12, 0X000000, null, null, null, null, null, TextFormatAlign.CENTER);
			this.txtCombo.selectable = false;
			this.txtCombo.x = 0;
			this.txtCombo.y = 2;
			this.txtCombo.width = 50;
			this.txtCombo.height = 16;
			
			this.block = new ComboBox();
			this.block.addEventListener(Event.SELECT, onSelect);
			this.block.x = 190;
			this.block.y = 110;
			this.block.width = 50;
			this.block.height = 20;
			this.block.listPaddingTop = 5;
			this.block.rowSpacing = 5;
			this.block.draw(0X000000, 0.2);
			this.block.bind(BlockComboItem, new ArrayList("是", "否"));
			this.block.addChild(this.txtCombo);
			
			this.btnSend = new CommonButton("发送");
			this.btnSend.x = 10;
			this.btnSend.y = 140;
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		override public function onAdd():void
		{
			Layer.instance.content.addChild(this.label_Title);
			Layer.instance.content.addChild(this.label_Queue);
			Layer.instance.content.addChild(this.label_Block);
			Layer.instance.content.addChild(this.label_params);
			Layer.instance.content.addChild(this.txtQueue);
			Layer.instance.content.addChild(this.txtParams);
			Layer.instance.content.addChild(this.block);
			Layer.instance.content.addChild(this.btnSend);
			
			this.btnSend.addEventListener(MouseEvent.CLICK, onSend);
		}
		
		override public function onRemove():void
		{
			Layer.instance.content.removeChild(this.label_Title);
			Layer.instance.content.removeChild(this.label_Queue);
			Layer.instance.content.removeChild(this.label_Block);
			Layer.instance.content.removeChild(this.label_params);
			Layer.instance.content.removeChild(this.txtQueue);
			Layer.instance.content.removeChild(this.txtParams);
			Layer.instance.content.removeChild(this.block);
			Layer.instance.content.removeChild(this.btnSend);
			
			this.btnSend.removeEventListener(MouseEvent.CLICK, onSend);
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		private function onSelect(event:Event):void
		{
			this.txtCombo.text = this.block.selected;
		}
		
		private function onSend(event:MouseEvent):void
		{
			var params:String = trim(this.txtParams.text);
			var queue:String = (trim(this.txtQueue.text) == "") ? null : trim(this.txtQueue.text);
			var isBlock:Boolean = (this.block.selected == "是");
			
			if (queue == null)
			{
				this.notify(MVCMethod.EXAMPLE_NOTICE, params, queue, isBlock);
			}
			else
			{
				for (var i:int = 0; i < 3; i++)
				{
					this.notify(MVCMethod.EXAMPLE_NOTICE, params, queue, isBlock);
				}
			}
		}
	
	}
}
